package com.bff.gaia.client.combine.planutil;

import com.bff.gaia.configuration.ConfigConstants;
import com.bff.gaia.configuration.CoreOptions;
import com.bff.gaia.configuration.GlobalConfiguration;

import java.io.File;

public class GetConfigure {

	private static final String CONFIG_DIRECTORY_FALLBACK_1 = "../conf";
	private static final String CONFIG_DIRECTORY_FALLBACK_2 = "conf";

	public static float getSlotSize(){
		return  GlobalConfiguration.loadConfiguration(getConfigurationDirectoryFromEnv()).getFloat(CoreOptions.DEFAULT_SLOT_SIZE);
		//return  GlobalConfiguration.loadConfiguration("E:\\gaia-1.8.0\\gaia-dist\\src\\main\\resources").getFloat(CoreOptions.DEFAULT_SLOT_SIZE);
	}

	public static String getHDFSMasterAddress(){
		String hdfsPath = "hdfs://";
		String masterIP = GlobalConfiguration.loadConfiguration(getConfigurationDirectoryFromEnv()).getString(CoreOptions.HDFS_MASTER);
		String masterPort = GlobalConfiguration.loadConfiguration(getConfigurationDirectoryFromEnv()).getString(CoreOptions.HDFS_PORT);

		hdfsPath = hdfsPath + masterIP+":"+ masterPort;

		return hdfsPath;
	}

	public static String getConfigurationDirectoryFromEnv() {
		String location = System.getenv(ConfigConstants.ENV_GAIA_CONF_DIR);

		if (location != null) {
			if (new File(location).exists()) {
				return location;
			}
			else {
				throw new RuntimeException("The configuration directory '" + location + "', specified in the '" +
					ConfigConstants.ENV_GAIA_CONF_DIR + "' environment variable, does not exist.");
			}
		}
		else if (new File(CONFIG_DIRECTORY_FALLBACK_1).exists()) {
			location = CONFIG_DIRECTORY_FALLBACK_1;
		}
		else if (new File(CONFIG_DIRECTORY_FALLBACK_2).exists()) {
			location = CONFIG_DIRECTORY_FALLBACK_2;
		}
		else {
			throw new RuntimeException("The configuration directory was not specified. " +
				"Please specify the directory containing the configuration file through the '" +
				ConfigConstants.ENV_GAIA_CONF_DIR + "' environment variable.");
		}
		return location;
	}

}